package com.shanghaionstar.user.presenter;

import android.content.Context;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.framework.mvp.base.IMVPScheduler;
import com.onstar.cn.framework.mvp.base.IMVPView;
import com.onstar.cn.framework.mvp.base.core.BasePresenter;
import com.onstar.cn.more.response.GetCardHistoryResponse;
import com.shanghaionstar.user.iview.IActivieHistiryView;
import com.shanghaionstar.user.service.ActiveHistoryService;

import rx.functions.Action1;

/**
 * Created by Mengzhou Gu on 2016/8/3.
 */
public class ActiveHistoryPresenter extends BasePresenter<GetCardHistoryResponse> {
    private Context context;

    private IActivieHistiryView activieHistiryView;
    private ActiveHistoryService service;

    public ActiveHistoryPresenter(IMVPScheduler mvpScheduler,Context context) {
        super(mvpScheduler);
        this.context=context;
    }

    @Override
    public void setView(IMVPView<GetCardHistoryResponse> view) {
        super.setView(view);
        activieHistiryView = (IActivieHistiryView) view;
        service = new ActiveHistoryService(context);
        setServiceProvider(service);
    }

    public void getData(int currentPage){
        activieHistiryView.setLoading(true);
        subscription=service.getObserval(currentPage).subscribeOn(mvpScheduler.backgroundThread()).observeOn(mvpScheduler.mainThread())
                .subscribe(new Action1<GetCardHistoryResponse>() {
                    @Override
                    public void call(GetCardHistoryResponse getCardHistoryResponse) {
                        if(!CommonUtil.isNull(getCardHistoryResponse)){
                            activieHistiryView.setLoading(false);
                            activieHistiryView.showSuccess(getCardHistoryResponse);
                        }else{
                            activieHistiryView.setLoading(false);
                            activieHistiryView.showFailure();
                        }

                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        activieHistiryView.setLoading(false);
                        activieHistiryView.showError(throwable);
                    }
                });
    }
}
